package com.wing.dict.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.constant.GlobalConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.dict.model.entity.DictLabel;
import com.wing.dict.service.DictLabelService;
import com.wing.redis.utils.redis.RedisHelperImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(value = "系统字典标签模块API", tags = "#/dict/dictLabel")
@RequestMapping("dict/admin/dictLabel")
public class DictLabelController {

    @Autowired
    private DictLabelService dictLabelService;

    @Autowired
    private RedisHelperImpl redisHelper;

    /**
     * 字典标签列表
     */
    @ApiOperation(value = "字典标签", notes = "列表")
    @GetMapping("/list")
    PageResult<DictLabel> list(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(required = false, name = "labelName") String labelName
            , @RequestParam(required = false, name = "dictType") String dictType
            , @RequestParam(required = false, name = "labelStatus") String labelStatus
            , @RequestParam(required = false, name = "requestType") String requestType) {
        QueryWrapper<DictLabel> dictQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dictType)) {
            dictQueryWrapper.eq("dict_type", dictType);
        }
        if (StringUtils.isNotBlank(labelName)) {
            dictQueryWrapper.like("label_name", labelName);
        }
        if (StringUtils.isNotBlank(labelStatus)) {
            dictQueryWrapper.eq("status", labelStatus);
        }

        if (requestType != null && "research".equals(requestType)) {//研发
            List<Integer> labelValueList = new ArrayList<>();
            labelValueList.add(1);
            labelValueList.add(2);
            labelValueList.add(7);
            dictQueryWrapper.in("label_value", labelValueList);
        }else if (requestType != null && "product".equals(requestType)) {//生产
            List<Integer> labelValueList = new ArrayList<>();
            labelValueList.add(1);
            labelValueList.add(2);
            labelValueList.add(7);
            labelValueList.add(9);
            dictQueryWrapper.in("label_value", labelValueList);
        }else if (requestType != null && "sell".equals(requestType)) {//销售
            List<Integer> labelValueList = new ArrayList<>();
            labelValueList.add(2);
            dictQueryWrapper.in("label_value", labelValueList);
        }else if (requestType != null && "purchase".equals(requestType)) {//采购
            List<Integer> labelValueList = new ArrayList<>();
            labelValueList.add(1);
            labelValueList.add(3);
            labelValueList.add(6);
            labelValueList.add(8);
            dictQueryWrapper.in("label_value", labelValueList);
        }else if (requestType != null && "storage".equals(requestType)) {//仓储

        }else if (requestType != null && "finance".equals(requestType)) {//财务

        }

        dictQueryWrapper.orderByAsc("sort");
        Page<DictLabel> pg = new Page<>(page, limit);
        IPage<DictLabel> list = dictLabelService.page(pg, dictQueryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 字典标签列表
     */
    @ApiOperation(value = "字典标签", notes = "下拉框列表")
    @GetMapping("/select")
    JsonResult select(@RequestParam(required = false, name = "dictType") String dictType) {
        QueryWrapper<DictLabel> dictQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dictType)) {
            dictQueryWrapper.eq("dict_type", dictType);
        }
        dictQueryWrapper.eq("status", "0");
        dictQueryWrapper.orderByAsc("sort");
        return JsonResult.success("查询成功", dictLabelService.list(dictQueryWrapper));
    }


    @ApiOperation(value = "添加字典类型", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@Validated DictLabel dictLabel) {
        dictLabel.setCreateTime(new Date());
        if (dictLabelService.save(dictLabel)) {
//            redisHelper.remove(GlobalConstants.DICT_LABEL_KEY);
//            redisHelper.valuePut(GlobalConstants.DICT_LABEL_KEY, dictLabelService.list());
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "更新字典标签", notes = "更新")
    @PutMapping("/update")
    public JsonResult update(@Validated DictLabel dictLabel) {
        if (dictLabelService.updateById(dictLabel)) {
//            redisHelper.remove(GlobalConstants.DICT_LABEL_KEY);
//            redisHelper.valuePut(GlobalConstants.DICT_LABEL_KEY, dictLabelService.list());
            return JsonResult.success("更新成功");
        }
        return JsonResult.fail("更新失败");
    }

    @ApiOperation(value = "删除字典标签", notes = "删除")
    @DeleteMapping("/del/{id}")
    public JsonResult del(@PathVariable("id") Long id) {
        if (dictLabelService.removeById(id)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
